home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / cips3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-29  |  13.7 KB  |  499 lines

  1.  
  2.    /*************************** 
  3.    * 
  4.    *   cips3.c 
  5.    *   COMPOSITE FILE COMPRISING: 
  6.    *   addsub.c 
  7.    *   cutp.c 
  8.    *   rotate.c 
  9.    * 
  10.    ***************************\ 
  11.  
  12.  
  13.  
  14.  
  15.    /***********************************************
  16.    *
  17.    *       file d:\cips\addsub.c
  18.    *
  19.    *       Functions: This file contains
  20.    *          add_image_array
  21.    *          subtract_image_array
  22.    *
  23.    *       Purpose:
  24.    *          These functions implement
  25.    *          image addition and subtraction.
  26.    *
  27.    *       External Calls:
  28.    *          wtiff.c - round_off_image_size
  29.    *                    create_file_if_needed
  30.    *                    write_array_into_tiff_image
  31.    *          tiff.c - read_tiff_header
  32.    *          rtiff.c - read_tiff_image
  33.    *
  34.    *
  35.    *       Modifications:
  36.    *          1 April 1992 - created
  37.    *
  38.    *************************************************/
  39.  
  40. #include "cips.h"
  41.  
  42.      /*******************************************
  43.      *
  44.      *   add_image_array(...
  45.      *
  46.      *   This function adds two ROWSxCOLS image
  47.      *   sections.  The image file named out_name
  48.      *   will receive the sum of the image file
  49.      *   named in1_name and the image file
  50.      *   named in2_name.
  51.      *
  52.      *******************************************/
  53.  
  54.  
  55. add_image_array(in1_name, in2_name, out_name, 
  56.                 the_image, out_image,
  57.           il1, ie1, ll1, le1,
  58.           il2, ie2, ll2, le2,
  59.           il3, ie3, ll3, le3)
  60.    char   in1_name[], in2_name[], out_name[];
  61.    int    il1, ie1, ll1, le1,
  62.           il2, ie2, ll2, le2,
  63.           il3, ie3, ll3, le3;
  64.    short  the_image[ROWS][COLS],
  65.           out_image[ROWS][COLS];
  66.  
  67. {
  68.    int    i, j, length, max, width;
  69.    struct tiff_header_struct image_header;
  70.  
  71.    create_file_if_needed(in1_name, out_name, out_image);
  72.  
  73.    read_tiff_header(in1_name, &image_header);
  74.  
  75.    max = 255;
  76.    if(image_header.bits_per_pixel == 4)
  77.       max = 16;
  78.  
  79.    read_tiff_image(in1_name, the_image, 
  80.                    il1, ie1, ll1, le1);
  81.    read_tiff_image(in2_name, out_image, 
  82.                    il2, ie2, ll2, le2);
  83.  
  84.    for(i=0; i<ROWS; i++){
  85.       for(j=0; j<COLS; j++){
  86.          out_image[i][j] = the_image[i][j] + 
  87.                            out_image[i][j];
  88.          if(out_image[i][j] > max)
  89.             out_image[i][j] = max;
  90.       }  /* ends loop over j */
  91.    }  /* ends loop over i */
  92.  
  93.    write_array_into_tiff_image(out_name, out_image,
  94.                                il3, ie3, ll3, le3);
  95.  
  96. }  /* ends add_image_array */
  97.  
  98.  
  99.  
  100.  
  101.  
  102.      /*******************************************
  103.      *
  104.      *   subtract_image_array(...
  105.      *
  106.      *   This function subtracts two ROWSxCOLS image
  107.      *   sections.  The image file named out_name
  108.      *   will receive the difference of the image file
  109.      *   named in1_name and the image file
  110.      *   named in2_name.
  111.      *
  112.      *   out_name = in1_name - in2_name
  113.      *
  114.      *******************************************/
  115.  
  116.  
  117. subtract_image_array(in1_name, in2_name, out_name, 
  118.                      the_image, out_image,
  119.           il1, ie1, ll1, le1,
  120.           il2, ie2, ll2, le2,
  121.           il3, ie3, ll3, le3)
  122.    char   in1_name[], in2_name[], out_name[];
  123.    int    il1, ie1, ll1, le1,
  124.           il2, ie2, ll2, le2,
  125.           il3, ie3, ll3, le3;
  126.    short  the_image[ROWS][COLS],
  127.           out_image[ROWS][COLS];
  128.  
  129. {
  130.    int    i, j, length, width;
  131.    struct tiff_header_struct image_header;
  132.  
  133.  
  134.    create_file_if_needed(in1_name, out_name, out_image);
  135.  
  136.    read_tiff_header(in1_name, &image_header);
  137.  
  138.    read_tiff_image(in1_name, the_image, 
  139.                    il1, ie1, ll1, le1);
  140.    read_tiff_image(in2_name, out_image, 
  141.                    il2, ie2, ll2, le2);
  142.  
  143.    for(i=0; i<ROWS; i++){
  144.       for(j=0; j<COLS; j++){
  145.          out_image[i][j] = the_image[i][j] - 
  146.                            out_image[i][j];
  147.          if(out_image[i][j] < 0)
  148.             out_image[i][j] = 0;
  149.       }  /* ends loop over j */
  150.    }  /* ends loop over i */
  151.  
  152.    write_array_into_tiff_image(out_name, out_image,
  153.                                il3, ie3, ll3, le3);
  154.  
  155. }  /* ends subtract_image_array */
  156.  
  157.     /***********************************************
  158.     *
  159.     *       file d:\cips\cutp.c
  160.     *
  161.     *       Functions: This file contains
  162.     *          cut_image_piece
  163.     *          paste_image_piece
  164.     *          check_cut_and_paste_limits
  165.     *
  166.     *       Purpose:
  167.     *          These functions cut pieces out
  168.     *          of images and paste them back into
  169.     *          images.
  170.     *
  171.     *       External Calls:
  172.     *          wtiff.c - does_not_exist
  173.     *                    round_off_image_size
  174.     *                    create_file_if_needed
  175.     *                    write_array_into_tiff_image
  176.     *          tiff.c - read_tiff_header
  177.     *          rtiff.c - read_tiff_image
  178.     *
  179.     *
  180.     *       Modifications:
  181.     *          3 April 1992 - created
  182.     *
  183.     *************************************************/
  184.  
  185.  
  186.      /*******************************************
  187.      *
  188.      *   cut_image_piece(...
  189.      *
  190.      *   This function cuts out a rectangular
  191.      *   piece of an image.  This rectangle can
  192.      *   be any shape so long as no dimension
  193.      *   is greater than ROWS or COLS.
  194.      *
  195.      *******************************************/
  196.  
  197.  
  198. cut_image_piece(name, the_image, il, ie, ll, le)
  199.    char   name[];
  200.    int    il, ie, ll, le;
  201.    short  the_image[ROWS][COLS];
  202.  
  203. {
  204.    if(does_not_exist(name)){
  205.       printf("\n\ncut_image_piece>> ERROR "
  206.              "image file does not exist %s", name);
  207.       return(-1);
  208.    }  /* ends if does_not_exist */
  209.  
  210.    read_tiff_image(name, the_image, il, ie, ll, le);
  211.  
  212. }  /* ends cut_image_piece */
  213.  
  214.  
  215.  
  216.  
  217.      /*******************************************
  218.      *
  219.      *   paste_image_piece(...
  220.      *
  221.      *   This function pastes a rectangular
  222.      *   piece of an image into another image.
  223.      *   This rectangle can be any shape so long
  224.      *   as no dimension is greater than ROWS or COLS.
  225.      *   The rectangle to be pasted into the image
  226.      *   is described by the il, ie, ll, le
  227.      *   parameters.
  228.      *
  229.      *   You pass is the out_image array just in
  230.      *   case you need to allocate the destination
  231.      *   image.
  232.      *
  233.      *******************************************/
  234.  
  235.  
  236. paste_image_piece(dest_name, source_name, the_image,
  237.                   out_image, il, ie, ll, le)
  238.    char   dest_name[], source_name[];
  239.    int    il, ie, ll, le;
  240.    short  the_image[ROWS][COLS],
  241.           out_image[ROWS][COLS];
  242.  
  243. {
  244.    struct tiff_header_struct image_header;
  245.  
  246.    create_file_if_needed(source_name, dest_name, 
  247.                          out_image);
  248.  
  249.    write_array_into_tiff_image(dest_name, the_image,
  250.                                il, ie, ll, le);
  251.  
  252. }  /* ends paste_image_piece */
  253.  
  254.  
  255.  
  256.      /*******************************************
  257.      *
  258.      *   check_cut_and_paste_limits(...
  259.      *
  260.      *   This function looks at the line and
  261.      *   element parameters and ensures that they
  262.      *   are not bigger than ROWS and COLS.  If
  263.      *   they are bigger, the last element or
  264.      *   last line parameters are reduced.
  265.      *
  266.      *******************************************/
  267.  
  268. check_cut_and_paste_limits(il, ie, ll, le)
  269.    int *il, *ie, *ll, *le;
  270. {
  271.    if((*ll - *il) > ROWS)
  272.       *ll = *il + ROWS;
  273.    if((*le - *ie) > COLS)
  274.       *le = *ie + COLS;
  275. }  /* ends check_cut_and_paste_limits */
  276.  
  277.  
  278.     /***********************************************
  279.     *
  280.     *       file d:\cips\rotate.c
  281.     *
  282.     *       Functions: This file contains
  283.     *          rotate_flip_image_array
  284.     *
  285.     *       Purpose:
  286.     *          This function rotates or flips an image
  287.     *          array in one of five ways.
  288.     *
  289.     *       External Calls:
  290.     *          wtiff.c - round_off_image_size
  291.     *                    create_file_if_needed
  292.     *                    write_array_into_tiff_image
  293.     *          tiff.c - read_tiff_header
  294.     *          rtiff.c - read_tiff_image
  295.     *
  296.     *
  297.     *       Modifications:
  298.     *          1 April 1992 - created
  299.     *
  300.     *************************************************/
  301.  
  302.  
  303.      /*******************************************
  304.      *
  305.      *   rotate_flip_image_array(...
  306.      *
  307.      *   This function rotates an image array
  308.      *   in one of three ways or flips an image
  309.      *   array either vertically or horizontally.
  310.      *   The rotation_type parameter specifies
  311.      *   the operation.  When rotation_type is
  312.      *   1, 2, or 3 you rotate.  When it is
  313.      *   4 or 5 you flip.
  314.      *
  315.      *   I define rotation as this:  Pin down the
  316.      *   lower left hand corner of the image array
  317.      *   and rotate the image 90 degrees clockwise.
  318.      *   1 rotation is 90 degrees, 2 rotations are
  319.      *   180 degrees, and 3 rotations are 270 degrees.
  320.      *   4 rotations bring you back to where you
  321.      *   started.
  322.      *
  323.      *   The cases are:
  324.      *
  325.      *   If the input image array is:
  326.      *        1 2 3
  327.      *        4 5 6
  328.      *        7 8 9
  329.      *
  330.      *   Rotate # 1 - the result is:
  331.      *        7 4 1
  332.      *        8 5 2
  333.      *        9 6 3
  334.      *
  335.      *   Rotate # 2 - the result is:
  336.      *        9 8 7
  337.      *        6 5 4
  338.      *        3 2 1
  339.      *
  340.      *   Rotate # 3 - the result is:
  341.      *        3 6 9
  342.      *        2 5 8
  343.      *        1 4 7
  344.      *
  345.      *   Flip # 4 - horizontal the result is:
  346.      *        3 2 1
  347.      *        6 5 4
  348.      *        9 8 7
  349.      *
  350.      *   Flip # 5 - vertical the result is:
  351.      *        7 8 9
  352.      *        4 5 6
  353.      *        1 2 3
  354.      *
  355.      *
  356.      *   The in_file is the source image with
  357.      *   parameters given by il1, ie1, ll1, le1.
  358.      *
  359.      *   The out_file is the destination image with
  360.      *   parameters given by il2, ie2, ll2, le2.
  361.      *
  362.      *******************************************/
  363.  
  364.  
  365. rotate_flip_image_array(in_name, out_name, the_image,
  366.                    out_image, il1, ie1, ll1, le1,
  367.                    il2, ie2, ll2, le2, rotation_type)
  368.    char  in_name[], out_name[];
  369.    int   il1, ie1, ll1, le1,
  370.          il2, ie2, ll2, le2, rotation_type;
  371.    short the_image[ROWS][COLS],
  372.          out_image[ROWS][COLS];
  373. {
  374.    int    cd2, i, j, length, rd2, type, width;
  375.    struct tiff_header_struct image_header;
  376.  
  377.    create_file_if_needed(in_name, out_name, out_image);
  378.  
  379.      /*******************************************
  380.      *
  381.      *   Check the rotation_type.  If it is not
  382.      *   a valid value, set it to 1.
  383.      *
  384.      *******************************************/
  385.  
  386.    type = rotation_type;
  387.    if(type != 1  &&
  388.       type != 2  &&
  389.       type != 3  &&
  390.       type != 4  &&
  391.       type != 5) type = 1;
  392.  
  393.    read_tiff_image(in_name, the_image, 
  394.                    il1, ie1, ll1, le1);
  395.  
  396.      /*******************************************
  397.      *
  398.      *   Rotate the image array as desired.
  399.      *
  400.      *******************************************/
  401.  
  402.      /*******************************************
  403.      *
  404.      *   1 90 degree rotation
  405.      *
  406.      *******************************************/
  407.  
  408.    if(type == 1  ||  type == 2  ||  type == 3){
  409.       for(i=0; i<ROWS; i++){
  410.          for(j=0; j<COLS; j++)
  411.             out_image[j][COLS-1-i] = the_image[i][j];
  412.       }  /* ends loop over i */
  413.    }  /* ends if type == 1 or 2 or 3 */
  414.  
  415.  
  416.      /*******************************************
  417.      *
  418.      *   a second 90 degree rotation
  419.      *
  420.      *******************************************/
  421.  
  422.    if(type == 2  ||  type == 3){
  423.       for(i=0; i<ROWS; i++)
  424.          for(j=0; j<COLS; j++)
  425.             the_image[i][j] = out_image[i][j];
  426.       for(i=0; i<ROWS; i++){
  427.          for(j=0; j<COLS; j++)
  428.             out_image[j][COLS-1-i] = the_image[i][j];
  429.       }  /* ends loop over i */
  430.    }  /* ends if type == 2 or 3 */
  431.  
  432.  
  433.      /*******************************************
  434.      *
  435.      *   a third 90 degree rotation
  436.      *
  437.      *******************************************/
  438.  
  439.    if(type == 3){
  440.       for(i=0; i<ROWS; i++)
  441.          for(j=0; j<COLS; j++)
  442.             the_image[i][j] = out_image[i][j];
  443.       for(i=0; i<ROWS; i++){
  444.          for(j=0; j<COLS; j++)
  445.             out_image[j][COLS-1-i] = the_image[i][j];
  446.       }  /* ends loop over i */
  447.    }  /* ends if type == 3 */
  448.  
  449.  
  450.      /*******************************************
  451.      *
  452.      *   Flip the image array horizontally
  453.      *   about the center vertical axis.
  454.      *
  455.      *******************************************/
  456.  
  457.    if(type == 4){
  458.       cd2 = COLS/2;
  459.       for(j=0; j<cd2; j++){
  460.          for(i=0; i<ROWS; i++){
  461.             out_image[i][COLS-1-j] = the_image[i][j];
  462.          }  /* ends loop over i */
  463.       }  /* ends loop over j */
  464.  
  465.       for(j=cd2; j<COLS; j++){
  466.          for(i=0; i<ROWS; i++){
  467.             out_image[i][COLS-1-j] = the_image[i][j];
  468.          }  /* ends loop over i */
  469.       }  /* ends loop over j */
  470.    }  /* ends if type == 4 */
  471.  
  472.  
  473.      /*******************************************
  474.      *
  475.      *   Flip the image array vertically
  476.      *   about the center horizontal axis.
  477.      *
  478.      *******************************************/
  479.  
  480.    if(type == 5){
  481.       rd2 = ROWS/2;
  482.       for(i=0; i<rd2; i++){
  483.          for(j=0; j<COLS; j++){
  484.             out_image[ROWS-1-i][j] = the_image[i][j];
  485.          }  /* ends loop over j */
  486.       }  /* ends loop over i */
  487.  
  488.       for(i=rd2; i<ROWS; i++){
  489.          for(j=0; j<COLS; j++){
  490.             out_image[ROWS-1-i][j] = the_image[i][j];
  491.          }  /* ends loop over j */
  492.       }  /* ends loop over i */
  493.    }  /* ends if type == 5 */
  494.  
  495.    write_array_into_tiff_image(out_name, out_image,
  496.                                il2, ie2, ll2, le2);
  497.  
  498. }  /* ends rotate_flip_image_array */
  499.